package smap;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import BD.DisciplinaBD;
import DAO.DisciplinaDAO;

public class TelaDisciplina extends javax.swing.JDialog {
    
    DisciplinaBD dis = new DisciplinaBD();
    DisciplinaDAO disDao = new DisciplinaDAO();
    
    public TelaDisciplina(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GrupoPesquisa = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        btnProfessor = new javax.swing.JButton();
        btnUsuario = new javax.swing.JButton();
        btnCurso = new javax.swing.JButton();
        btnDisciplina = new javax.swing.JButton();
        btnAluno = new javax.swing.JButton();
        btnBackup = new javax.swing.JButton();
        btnTurma = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnVoltar = new javax.swing.JButton();
        pnlControle = new javax.swing.JPanel();
        txtPesquisaDisciplina = new javax.swing.JTextField();
        cmbPesquisa = new javax.swing.JComboBox();
        btnPesquisar = new javax.swing.JButton();
        btnCarregartodos = new javax.swing.JButton();
        btnCadastrar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDisciplina = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tela Disciplina");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/professor-icon.png"))); // NOI18N
        btnProfessor.setText("Professor");
        btnProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProfessorActionPerformed(evt);
            }
        });

        btnUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/user-icon.png"))); // NOI18N
        btnUsuario.setText("Usuario");
        btnUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUsuarioActionPerformed(evt);
            }
        });

        btnCurso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/curso-icon.png"))); // NOI18N
        btnCurso.setText("Curso");
        btnCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCursoActionPerformed(evt);
            }
        });

        btnDisciplina.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/disciplina-icon.png"))); // NOI18N
        btnDisciplina.setText("Disciplina");
        btnDisciplina.setEnabled(false);

        btnAluno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/aluno-icon.png"))); // NOI18N
        btnAluno.setText("Aluno");
        btnAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlunoActionPerformed(evt);
            }
        });

        btnBackup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/backup-icon.png"))); // NOI18N
        btnBackup.setText("Backup");
        btnBackup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackupActionPerformed(evt);
            }
        });

        btnTurma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/turma-icon.png"))); // NOI18N
        btnTurma.setText("Turma");
        btnTurma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTurmaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnProfessor)
                    .addComponent(btnUsuario)
                    .addComponent(btnAluno)
                    .addComponent(btnBackup)
                    .addComponent(btnDisciplina)
                    .addComponent(btnCurso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnTurma))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar-icon.png"))); // NOI18N
        btnVoltar.setText("Voltar");
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(514, 514, 514))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlControle.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)), "Pesquisar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.darkGray));

        cmbPesquisa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome da Disciplina", "Carga Horária", "Tipo da Disciplina", "Área" }));

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/procurar-icon.png"))); // NOI18N
        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnCarregartodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/todos-icon.png"))); // NOI18N
        btnCarregartodos.setText("Carregar todos");
        btnCarregartodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarregartodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlControleLayout = new javax.swing.GroupLayout(pnlControle);
        pnlControle.setLayout(pnlControleLayout);
        pnlControleLayout.setHorizontalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtPesquisaDisciplina)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCarregartodos, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlControleLayout.setVerticalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPesquisaDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisar)
                    .addComponent(btnCarregartodos))
                .addGap(23, 23, 23))
        );

        btnCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/add-icon.png"))); // NOI18N
        btnCadastrar.setText("Cadastrar");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/delete-icon.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alterar-icon.png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        tblDisciplina.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome", "Carga Horária", "Tipo", "Créditos", "Área"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblDisciplina.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblDisciplina.getTableHeader().setReorderingAllowed(false);
        tblDisciplina.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDisciplinaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblDisciplina);
        tblDisciplina.getColumnModel().getColumn(0).setMinWidth(70);
        tblDisciplina.getColumnModel().getColumn(0).setPreferredWidth(60);
        tblDisciplina.getColumnModel().getColumn(0).setMaxWidth(70);
        tblDisciplina.getColumnModel().getColumn(2).setMinWidth(120);
        tblDisciplina.getColumnModel().getColumn(2).setPreferredWidth(120);
        tblDisciplina.getColumnModel().getColumn(2).setMaxWidth(130);
        tblDisciplina.getColumnModel().getColumn(3).setMinWidth(110);
        tblDisciplina.getColumnModel().getColumn(3).setPreferredWidth(100);
        tblDisciplina.getColumnModel().getColumn(3).setMaxWidth(110);
        tblDisciplina.getColumnModel().getColumn(4).setMinWidth(100);
        tblDisciplina.getColumnModel().getColumn(4).setPreferredWidth(100);
        tblDisciplina.getColumnModel().getColumn(4).setMaxWidth(120);
        tblDisciplina.getColumnModel().getColumn(5).setMinWidth(250);
        tblDisciplina.getColumnModel().getColumn(5).setPreferredWidth(230);
        tblDisciplina.getColumnModel().getColumn(5).setMaxWidth(250);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlControle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(413, 413, 413)
                .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(pnlControle, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCadastrar)
                    .addComponent(btnExcluir)
                    .addComponent(btnAlterar))
                .addGap(12, 12, 12)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProfessorActionPerformed
        this.dispose(); //Fecha a Tela de Disciplina
        TelaProfessor pro = new TelaProfessor(null, true);
        pro.carregarTabela("");
        pro.setVisible(true); // Abre Tela Professor
    }//GEN-LAST:event_btnProfessorActionPerformed

    private void btnUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUsuarioActionPerformed
        this.dispose();  //Fecha a Tela de Disciplina
        TelaUsuario usu = new TelaUsuario(null, true);
        usu.carregarTabela("");
        usu.setVisible(true); //Abre Tela Usuario
    }//GEN-LAST:event_btnUsuarioActionPerformed

    private void btnCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCursoActionPerformed
        this.dispose(); //Fecha a Tela de Disciplina
        TelaCurso cur = new TelaCurso(null, true);
        cur.carregarTabela("");
        cur.setVisible(true); // Abre Tela Curso
    }//GEN-LAST:event_btnCursoActionPerformed

    private void btnAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlunoActionPerformed
        this.dispose(); //Fecha a Tela de Disciplina
        TelaAluno alu = new TelaAluno(null, true);
        alu.carregarTabela("");
        alu.setVisible(true); // Abre Tela Aluno
    }//GEN-LAST:event_btnAlunoActionPerformed

    private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupActionPerformed
        this.dispose(); //Fecha a Tela de Disciplina
        TelaBackup bac = new TelaBackup(null, true);
        bac.setVisible(true); // Abre Tela Backup
    }//GEN-LAST:event_btnBackupActionPerformed

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        this.dispose(); //Fecha a Tela de Disciplina
    }//GEN-LAST:event_btnVoltarActionPerformed

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        this.enable(); // Desabilita a Tela
        TelaCadDisciplina caddis = new TelaCadDisciplina(null, true);
        caddis.setCadastrar(true); //true para cadastrar
        caddis.autoIncremento();
        caddis.carregarComboArea();
        caddis.setVisible(true); // Abre a Tela de Cadastrar
        //Recarregar a tabela
        carregarTabela("");
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if(tblDisciplina.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null,"Selecione uma disciplina para alterar", 
                    "Selecione a disciplina", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {          
            //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblDisciplina.getSelectedRow();

            dis.setCodigo((Integer)tblDisciplina.getValueAt(linha, 0));
            dis.setNome((String)tblDisciplina.getValueAt(linha, 1));
            dis.setCargahoraria((Integer)tblDisciplina.getValueAt(linha, 2));
            dis.setTipodisciplina((String)tblDisciplina.getValueAt(linha, 3));
            dis.setCreditos((Integer)tblDisciplina.getValueAt(linha, 4));
            dis.setNomeArea((String)tblDisciplina.getValueAt(linha, 5));           
            
            //Manda os dados para a tela de Alterar
            this.enable();
            TelaCadDisciplina altdis = new TelaCadDisciplina(null, true);
            altdis.setCadastrar(false); //false para alterar
            altdis.setTitle("Alterar Disciplina");
            altdis.carregarComboArea();
            altdis.recebeObjejo(dis);
            altdis.setVisible(true);
            //Recarregar a tabela
            carregarTabela(txtPesquisaDisciplina.getText().trim());
        }      
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if(tblDisciplina.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null,"Selecione uma disciplina para excluir",
                    "Selecione a disciplina", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            Object[] opcoes = { "Sim", "Não" };
             //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblDisciplina.getSelectedRow();
            Integer resposta =JOptionPane.showOptionDialog(null,"Deseja excluir esta disciplina ?", 
                    "Exclusão",JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null,
                    opcoes, opcoes[1]);
            
            // Verifica Resposta do Usuário
            if(resposta == JOptionPane.OK_OPTION){
                //seta o código da disciplina para exclusão
                dis.setCodigo((Integer)tblDisciplina.getValueAt(linha, 0));
                
                //excluir passando o codigo
                if(disDao.excluirDisciplina(dis)){
                    JOptionPane.showMessageDialog(null,"Exclusão realizada com sucesso", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);
                    //recarregar o jTable
                    carregarTabela("");
                }
            }
            else{
                JOptionPane.showMessageDialog(null,"Exclusão cancelada", "Exclusão", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        carregarTabela(txtPesquisaDisciplina.getText().trim());
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnCarregartodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregartodosActionPerformed
        carregarTabela("");
    }//GEN-LAST:event_btnCarregartodosActionPerformed

    private void btnTurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTurmaActionPerformed
        this.dispose(); //Fecha a Tela de Disciplina
        TelaTurma tur = new TelaTurma(null, true);
        tur.carregarTabela("");
        tur.setVisible(true); // Abre Tela Turma
    }//GEN-LAST:event_btnTurmaActionPerformed

    private void tblDisciplinaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDisciplinaMouseClicked
        if(evt.getClickCount() == 2){
            if(tblDisciplina.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
                JOptionPane.showMessageDialog(null,"Selecione uma disciplina para visualizar", "Selecione a disciplina", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {          
                //Obtem os dados de acordo com a linha ao qual foi selecionada.
                Integer linha = tblDisciplina.getSelectedRow();

                dis.setCodigo((Integer)tblDisciplina.getValueAt(linha, 0));
                dis.setNome((String)tblDisciplina.getValueAt(linha, 1));
                dis.setCargahoraria((Integer)tblDisciplina.getValueAt(linha, 2));            
                dis.setTipodisciplina((String)tblDisciplina.getValueAt(linha, 3));
                dis.setCreditos((Integer)tblDisciplina.getValueAt(linha, 4));
                dis.setNomeArea((String)tblDisciplina.getValueAt(linha, 5));

                //Manda os dados para a tela de Alterar
                this.enable();
                TelaCadDisciplina visdis = new TelaCadDisciplina(null, true);
                visdis.carregarComboArea();
                visdis.visualizaObjeto(dis);
                visdis.setTitle("Visualizar Disciplina");
                visdis.setVisible(true);
            } 
        }
    }//GEN-LAST:event_tblDisciplinaMouseClicked

    public void carregarTabela(String campo){
        List<DisciplinaBD> listaDisciplina = new ArrayList<DisciplinaBD>(); 
        
        //Verifica qual opção esta selecionada no Combobox para realizar a pesquisa
        if(cmbPesquisa.getSelectedIndex() == 0){
            listaDisciplina = disDao.filtro("d.nome_dis", campo);
        }else{
            if(cmbPesquisa.getSelectedIndex() == 1){
                listaDisciplina = disDao.filtro("d.cargahoraria_dis", campo);
            }else{
                if(cmbPesquisa.getSelectedIndex() == 2){
                    listaDisciplina = disDao.filtro("d.tipo_dis", campo);
                }else{
                    listaDisciplina = disDao.filtro("ar.nome_area", campo);
                }
            }
       }
        
        //Define o Modelo da JTable
        DefaultTableModel tbm = (DefaultTableModel)tblDisciplina.getModel();
 
        //Remove a Tabela Antiga
        for(int i = tbm.getRowCount()-1; i >= 0; i--){ 
            tbm.removeRow(i); 
        } 
        int i = 0; 

        //Preenche a Tabela Nova
        for(DisciplinaBD us : listaDisciplina){
            tbm.addRow(new String[1]);
            tbm.setValueAt(us.getCodigo(), i, 0); //Objeto Linha Coluna
            tbm.setValueAt(us.getNome(), i, 1); 
            tbm.setValueAt(us.getCargahoraria(), i, 2);
            tbm.setValueAt(us.getTipodisciplina(), i, 3);
            tbm.setValueAt(us.getCreditos(), i, 4);
            tbm.setValueAt(us.getNomeArea(), i, 5);
            i++; 
        }
        tblDisciplina.setModel(tbm);
        centralizarTable(); // Centraliza alguns campos dentro da Tabela
    }
    
    public void centralizarTable(){
        //Centralizando alguns dados na Tabela
        DefaultTableCellRenderer center = new DefaultTableCellRenderer();
        center.setHorizontalAlignment(SwingConstants.CENTER ); 
        tblDisciplina.getColumnModel().getColumn(0).setCellRenderer(center);
        tblDisciplina.getColumnModel().getColumn(2).setCellRenderer(center);
        tblDisciplina.getColumnModel().getColumn(3).setCellRenderer(center);
        tblDisciplina.getColumnModel().getColumn(4).setCellRenderer(center);
    }
  
    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaDisciplina dialog = new TelaDisciplina(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup GrupoPesquisa;
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnAluno;
    private javax.swing.JButton btnBackup;
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCarregartodos;
    private javax.swing.JButton btnCurso;
    private javax.swing.JButton btnDisciplina;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnProfessor;
    private javax.swing.JButton btnTurma;
    private javax.swing.JButton btnUsuario;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JComboBox cmbPesquisa;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel pnlControle;
    private javax.swing.JTable tblDisciplina;
    private javax.swing.JTextField txtPesquisaDisciplina;
    // End of variables declaration//GEN-END:variables
}
